// pages/chat/chat.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toUserId:Number,
    name:String,
    avatar:String,
    user:Object,
    messages:Array,
    message:''
  },
  sendmessage(){
      let message={
        message:this.data.message,
        toUserId:this.data.toUserId
      }
      if(this.data.message==null){
        wx.showModal({
          title:"输入内容不为空"
        })
      }else{
        wx.onSocketOpen(res=>{
        })
        wx.sendSocketMessage({
          data:JSON.stringify(message),
          success:res=>{
            var user=getApp().globalData.user
            var sendmessage={
              sender:user.id,
              toUserId:this.data.toUserId,
              context:this.data.message,
              id:0
            }
            var messages=this.data.messages
            messages.push(sendmessage)
            this.setData({
              messages:messages,
              message:""
            })
          }
        })
      }
      
    },
    getmessage(e){
      this.setData({
        message:e.detail.value
      })
    },
    transformts(datetime:Date){
      var now=new Date()
      var datetimestr
      if(datetime.getMonth()-now.getMonth()>0){
        datetimestr=datetime.getMonth()-now.getMonth()+"月前"
     }else{
       if(datetime.getMonth()-now.getMonth()==0&&datetime.getDate()-now.getDate()>0){
          datetimestr=datetime.getDate()-now.getDate()+"日前"
       }else{
         if(datetime.getMonth()-now.getMonth()==0&&datetime.getDate()-now.getDate()==0&&datetime.getHours()-now.getHours()>0){
           datetimestr=datetime.getHours()-now.getHours()+"小时前"
         }else{
           if(datetime.getMonth()-now.getMonth()==0&&datetime.getDate()-now.getDate()==0&&datetime.getHours()-now.getHours()==0&&datetime.getMinutes()-now.getMinutes()){
             datetimestr=datetime.getMinutes()-now.getMinutes()+"分钟前"
           }else{
             datetimestr="一分钟前"
           }
         }
       }
     }
     return datetimestr
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    getApp().globalData.chat=this
    var toUserId=JSON.parse(option.toUserId)
    var name=option.name
    var avatar=option.avatar
    this.setData({
      toUserId:toUserId,
      name:name,
      avatar:avatar
    })
    var token=getApp().globalData.accessToken
    var that=this
    wx.request({
      url:"http://120.79.70.221:8080/user/messageDetail?toUser="+toUserId,
      method:"GET",
      header:{
        'content-type': 'application/json',
        'Authorization': 'Bearer '+token
    },
    success:res=>{
      var messages=[]
      for (let i = 0; i < res.data.length; i++) {
        const message = res.data[i];
        message.timestr=that.transformts(new Date(messages.ts))
        messages.push(message)
      }
      that.setData({
        messages:messages
      })
    }
    }) 
    var user=getApp().globalData.user
    this.setData({
      user:user
    })
    wx.connectSocket({
      url:"ws://120.79.70.221:8080/ws",
      header:{
        'Authorization': 'Bearer '+token
      },
      success:res=>{
        wx.onSocketMessage(res=>{
          var message=res.data
          message.timestr=that.transformts(message.ts)
          var messages=this.data.messages
          messages.push(message)
          this.setData({
            messages:messages
          })
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    wx.closeSocket()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})